//
//  GetOrderViewController.m
//  Manager
//
//  Created by 彭凯敏 on 15/8/25.
//  Copyright (c) 2015年 52xiaoluo. All rights reserved.
//

#import "GetOrderViewController.h"
#import "ViewRoomLogic.h"
#import "Viewroom.pb.h"


@interface GetOrderViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *contentView;


@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *expectLabel;
@property (weak, nonatomic) IBOutlet UILabel *liveLabel;


@property (weak, nonatomic) IBOutlet UILabel *roomLabel1;
@property (weak, nonatomic) IBOutlet UILabel *roomLabel2;
@property (weak, nonatomic) IBOutlet UILabel *roomStatus1;
@property (weak, nonatomic) IBOutlet UILabel *roomStatus2;


@property (weak, nonatomic) IBOutlet UILabel *jobLabel;

@property (weak, nonatomic) IBOutlet UILabel *remarkLabel;

- (IBAction)clickNewOrder:(UIButton *)sender;

@end

@implementation GetOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self baseCtlSetTitle:@"预约单详情"];
    
    [_contentView setSizeHeight:_contentView.sizeHeight];
    [_contentView setSizeWidth:ScreenWidth];
    
    _scrollView.contentSize = CGSizeMake(ScreenWidth,IS320?_contentView.sizeHeight: ScreenHeight-63.5);
    
    [_scrollView addSubview:_contentView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(requestSuccessSteward:) name:CMD_ViewRoom_StewardOrder object:nil];
    
    
    
    [self resetUI];
    
}


- (void)resetUI
{
    
    ViewOrder *_orderInfo = _model.order;
    
    if (!_orderInfo) {
        return;
    }
    _nameLabel.text = [_orderInfo.name judgeWithBase:@""];
    _timeLabel.text = [NSString stringWithFormat:@"预约时间：%@",[KMGetDate timeFromNum:[NSString stringWithFormat:@"%llu",_orderInfo.viewTime/1000] andFormStr:@"YYYY-MM-dd HH:mm"]];
    _expectLabel.attributedText = [[NSString stringWithFormat:@"%d",(int)_orderInfo.expectPrice]judgeAttributePrice:@"心理价位："];
    
    _jobLabel.attributedText = [_orderInfo.job judgeAttributeBase:@"职       业："];
    _liveLabel.attributedText = [_orderInfo.checkInDays judgeAttributeBase:@"入住时间："];
    
    if (!_orderInfo.viewRooms) {
        _roomLabel1.attributedText = [@"" judgeAttributeRoom:@"预约房间："];
        _roomStatus1.text = @"";
        _roomLabel2.text = @"";
        _roomStatus2.text = @"";
    }
    else
    {
        int i = 0;
        while (i<_orderInfo.viewRooms.count) {
            RoomInfo *info = _orderInfo.viewRooms[i];
            if (i == 0) {
                [UIViewCategory newRoomLabel:_roomLabel1 andInfo:info andView:_scrollView];
                _roomStatus1.text = [UIViewCategory roomStatusWith:info];
                _roomLabel2.text = @"";
                _roomStatus2.text = @"";
            }
            if (i == 1) {
                [UIViewCategory newRoomLabel:_roomLabel2 andInfo:info andView:_scrollView];
                _roomStatus2.text = [UIViewCategory roomStatusWith:info];
            }
            i++;
        }
    }
    
    [UIViewCategory remarkLabelWith:_remarkLabel andText:_orderInfo.remark andView:_scrollView ];
}



- (void)StewardOrderReqWithOrderID:(UInt32)orderID
{

    StewardOrderReqBuilder *builder = [[StewardOrderReqBuilder alloc]init];
    
    [builder setOrderStewardId:[[UserLogic shareInstance]getUserId]];
    
    [builder setViewOrderId:orderID];
    
    [[ViewRoomLogic shareInstance]stewardOrderReqWithBuild:builder.build];
    
}


- (void)GetNewOrder
{
    
    ViewOrderModel *model = _model;
    KMAlertView  *alert0 = [[KMAlertView alloc]initWithTitle:@"确认接单" message:@"这个客户我来搞定！" cancelButtonTitle:@"还是算了" clickButton:^(NSInteger index) {
        
        if (index == 1) {
            HUDShow(@"");
            [self StewardOrderReqWithOrderID:model.order.id];
        }
        
        
    } otherButtonTitles:@"确定"];
    
    [alert0 show];
}

- (void)requestSuccessSteward:(NSNotification *)noti
{
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if ([noti.object isKindOfClass:[ViewOrder class]])
        {
            ViewOrder *order = noti.object;
                HUDRemove;
            NSString *message = [NSString stringWithFormat:@"恭喜！您已成功接单，是否立即与客户%@(%@)联系？",order.name,order.phone];
            
            KMAlertView  *alert = [[KMAlertView alloc]initWithTitle:@"成功接单" message:message cancelButtonTitle:@"稍后再说" clickButton:^(NSInteger index) {
                
                if (index == 0) {
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }
                if (index == 1) {
                    
                    [KMCheckTel dialPhoneNumber:order.phone withView:self.view];
                }
                
            } otherButtonTitles:@"立即联系"];
            
            
            [alert show];
            
        }
        else if ([noti.object isKindOfClass:[ErrorModel class]])
        {
            ErrorModel *model = noti.object;
            [UIViewCategory FailShowWith:model];
        }
        
    });
}
- (IBAction)clickNewOrder:(UIButton *)sender {
    
    [self GetNewOrder];
    
}
@end
